
package com.ifcar.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ifcar.app.activity.HomePage;
import com.ifcar.app.db.DBUtils;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.rss.GetComments;
import com.ifcar.app.util.StringUtil;

public class CommentRank {
    public static List<Map<String, Object>> getRankCommentsFromDB(SQLiteDatabase db, String type) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

        String sql = "";
        if (type.equals("top")) {
            sql = "select * from " + NewsSQLiteOpenHelper.COMMENTS_LIST_TABLE + " where "
                    + NewsSQLiteOpenHelper.COMMENTS_COL_ID
                    + "=? order by comments_ptime desc limit 0,20";
        } else {
            sql = "select * from " + NewsSQLiteOpenHelper.COMMENTS_LIST_TABLE + " where "
                    + NewsSQLiteOpenHelper.COMMENTS_COL_ID + "=? limit 0,20";
        }

        Cursor cursor = db.rawQuery(sql, new String[] {
            type
        });

        if (cursor != null) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put(GetComments.DOCID_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_DOC_ID)));
                map.put(GetComments.AUTHOR_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_AUTHOR)));
                map.put(GetComments.REPLYCOUNT_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_REPLY_COUNT)));
                map.put(GetComments.TIE_TAG,
                        cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_TIE)));
                map.put(GetComments.TITLE_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_TITLE)));
                map.put(GetComments.REPLYBOARD_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_REPLY_BOARD)));
                map.put(GetComments.POSTID_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_POSTID)));
                map.put(GetComments.FLOOR_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.COMMENTS_FLOOR)));

                data.add(map);
            }
            cursor.close();
        }

        return data;
    }

    public static void insertCommentsListToDB(SQLiteDatabase db, List<Map<String, Object>> data,
            String type) {
        String docid = "";
        String replyCount = "";
        String title = "";
        String tie = "";
        String author = "";
        String replyBoard = "";
        String postid = "";
        String p = "";
        String ptime = "";

        db.beginTransaction();
        Cursor cursor = null;
        try {
            // TODO 为什么??
            if (!type.equals("top"))
                db.execSQL("delete from " + NewsSQLiteOpenHelper.COMMENTS_LIST_TABLE
                        + " where comments_col_id='" + type + "'");

            String sql = "select * from " + NewsSQLiteOpenHelper.COMMENTS_LIST_TABLE + " where "
                    + NewsSQLiteOpenHelper.COMMENTS_DOC_ID + "=? and "
                    + NewsSQLiteOpenHelper.COMMENTS_COL_ID + "='" + type + "'";
            for (int i = 0; i < data.size(); i++) {
                try {
                    if ((data.get(i)).get(GetComments.DOCID_TAG) != null) {
                        docid = StringUtil.GetString((data.get(i)).get(GetComments.DOCID_TAG));

                        cursor = db.rawQuery(sql, new String[] {
                            docid
                        });

                        if (cursor == null || cursor.getCount() == 0) {
                            if ((data.get(i)).get(GetComments.TIE_TAG) != null) {
                                tie = StringUtil.GetString((data.get(i)).get(GetComments.TIE_TAG));
                            }
                            if ((data.get(i)).get(GetComments.AUTHOR_TAG) != null) {
                                author = StringUtil.GetString((data.get(i)).get(GetComments.AUTHOR_TAG));
                            }
                            if ((data.get(i)).get(GetComments.REPLYCOUNT_TAG) != null) {
                                replyCount = StringUtil.GetString((data.get(i)).get(GetComments.REPLYCOUNT_TAG));
                            }
                            if ((data.get(i)).get(GetComments.TITLE_TAG) != null) {
                                title = StringUtil.GetString((data.get(i)).get(GetComments.TITLE_TAG));
                            }
                            if ((data.get(i)).get(GetComments.REPLYBOARD_TAG) != null) {
                                replyBoard = StringUtil.GetString((data.get(i)).get(GetComments.REPLYBOARD_TAG));
                            }
                            if ((data.get(i)).get(GetComments.POSTID_TAG) != null) {
                                postid = StringUtil.GetString((data.get(i)).get(GetComments.POSTID_TAG));
                            }
                            if ((data.get(i)).get(GetComments.FLOOR_TAG) != null) {
                                p = StringUtil.GetString((data.get(i)).get(GetComments.FLOOR_TAG));
                            }
                            if ((data.get(i)).get(GetComments.PTIME_TAG) != null) {
                                ptime = StringUtil.GetString((data.get(i)).get(GetComments.PTIME_TAG));
                            }
                            DBUtils.insertDataToCommentsListTable(db, type, docid, replyCount,
                                    title, tie, author, replyBoard, postid, p, ptime, "N");
                        }
                    } else if (!"top".equals(type)) {
                        if ((data.get(i)).get(GetComments.TIE_TAG) != null) {
                            tie = StringUtil.GetString((data.get(i)).get(GetComments.TIE_TAG));
                        }
                        if ((data.get(i)).get(GetComments.AUTHOR_TAG) != null) {
                            author = StringUtil.GetString((data.get(i)).get(GetComments.AUTHOR_TAG));
                        }
                        if ((data.get(i)).get(GetComments.REPLYCOUNT_TAG) != null) {
                            replyCount = StringUtil.GetString((data.get(i)).get(GetComments.REPLYCOUNT_TAG));
                        }
                        if ((data.get(i)).get(GetComments.TITLE_TAG) != null) {
                            title = StringUtil.GetString((data.get(i)).get(GetComments.TITLE_TAG));
                        }
                        if ((data.get(i)).get(GetComments.REPLYBOARD_TAG) != null) {
                            replyBoard = StringUtil.GetString((data.get(i)).get(GetComments.REPLYBOARD_TAG));
                        }
                        if ((data.get(i)).get(GetComments.POSTID_TAG) != null) {
                            postid = StringUtil.GetString((data.get(i)).get(GetComments.POSTID_TAG));
                        }
                        if ((data.get(i)).get(GetComments.FLOOR_TAG) != null) {
                            p = StringUtil.GetString((data.get(i)).get(GetComments.FLOOR_TAG));
                        }
                        DBUtils.insertDataToCommentsListTable(db, type, docid, replyCount, title,
                                tie, author, replyBoard, postid, p, "", "N");
                    }

                    if (null != cursor) {
                        cursor.close();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    if (null != cursor) {
                        cursor.close();
                    }
                }
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            // 当数据库操作出现错误时，需要捕获异常，结束事务
            db.endTransaction();
        }
        db.endTransaction();
    }

    public static List<Map<String, Object>> getRankComments(Context context, SQLiteDatabase db,
            String type, int start, int num) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        if (type.equals("top")) {
            data.addAll(GetComments.getTopCommentsList(start, num, context));
        } else if (type.equals("day")) {
            data.addAll(GetComments.getRankCommentsList("today", context));
        } else if (type.equals("week")) {
            data.addAll(GetComments.getRankCommentsList("week", context));
        }

        if (data.size() > 0) {
            insertCommentsListToDB(db, data, type);
        }

        return data;
    }
}
